//
//  LoginVC.m
//  PrivateSteward
//
//  Created by lanou3g on 16/3/25.
//  Copyright © 2016年 闫贺晨. All rights reserved.
//

#import "LoginVC.h"
#import "RegisterVC.h"
#import "UnloginPersonalHomePageVC.h"

@interface LoginVC ()<UITextFieldDelegate>

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"登陆";
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.loginBUtton.enabled = YES;
}


-(void)initView
{
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.myView.backgroundColor = [UIColor colorWithRed:124/255.0 green:213/255.0 blue:254/255.0 alpha:1];
    [self.view addSubview:self.myView];
    
    
    self.userImageview = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)/2 - 50, 50, 100, 100)];
    self.userImageview.image = [UIImage imageNamed:@"personal"];
    self.userImageview.layer.cornerRadius = 50;
    [self.myView addSubview:self.userImageview];
    
    
    self.userIDTextfiled = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.userImageview.frame) + 15, self.myView.frame.size.width, 40)];
    self.userIDTextfiled.delegate = self;
    self.userIDTextfiled.placeholder = @"  请输入账号";
    self.userIDTextfiled.textColor = [UIColor blackColor];
    self.userIDTextfiled.backgroundColor = [UIColor whiteColor];
    self.userIDTextfiled.enabled = YES;
    self.userIDTextfiled.returnKeyType = UIReturnKeyDone;
    self.userIDTextfiled.clearsOnBeginEditing = YES;
    self.userIDTextfiled.keyboardType = UIKeyboardAppearanceDefault;
    self.userIDTextfiled.clearButtonMode = UITextFieldViewModeAlways;
    [self.myView addSubview:self.userIDTextfiled];
    
    
    self.passwordTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.userIDTextfiled.frame), CGRectGetMaxY(self.userIDTextfiled.frame) + 1, self.userIDTextfiled.frame.size.width,self.userIDTextfiled.frame.size.height)];
    self.passwordTextFiled.delegate = self;
    self.passwordTextFiled.placeholder = @"  请输入密码";
    self.passwordTextFiled.textColor = [UIColor blackColor];
    self.passwordTextFiled.secureTextEntry = YES;
    self.passwordTextFiled.returnKeyType = UIReturnKeyDone;
    self.passwordTextFiled.keyboardType = UIKeyboardAppearanceDefault;
    self.passwordTextFiled.clearsOnBeginEditing = YES;
    self.passwordTextFiled.backgroundColor = [UIColor whiteColor];
    self.passwordTextFiled.enabled  = YES;
    [self.myView addSubview:self.passwordTextFiled];
    
    self.loginBUtton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.loginBUtton.backgroundColor = [UIColor blueColor];
    self.loginBUtton.frame  = CGRectMake(CGRectGetMinX(self.passwordTextFiled.frame) + 30, CGRectGetMaxY(self.passwordTextFiled.frame) + 10, CGRectGetWidth(self.passwordTextFiled.frame) - 60, CGRectGetHeight(self.passwordTextFiled.frame));
    [self.loginBUtton setTitle:@"登陆" forState:UIControlStateNormal];
    [self.loginBUtton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    self.loginBUtton.backgroundColor = [UIColor colorWithRed:158/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    self.loginBUtton.layer.cornerRadius = 8;
    [self.myView addSubview:self.loginBUtton];
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.registerButton.frame = CGRectMake(CGRectGetMaxX(self.loginBUtton.frame) - 150, CGRectGetMaxY(self.loginBUtton.frame) + 200, 180, 20);
    self.registerButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.registerButton setTitle:@"没有账号,注册登录" forState:UIControlStateNormal];
    self.registerButton.backgroundColor = self.myView.backgroundColor;
    [self.myView addSubview:self.registerButton];
    [self.registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)registerAction:(UIButton *)sender
{
    RegisterVC *registervc = [[RegisterVC alloc]init];
    [self.navigationController pushViewController:registervc animated:YES];
}

-(void)loginAction:(UIButton *)loginButton
{
    
    [AVUser logInWithMobilePhoneNumberInBackground:self.userIDTextfiled.text password:self.passwordTextFiled.text block:^(AVUser *user, NSError *error) {
        if (user != nil) {
            //登陆成功
            [[NSNotificationCenter defaultCenter]postNotificationName:@"didlogin" object:nil];
            self.loginBUtton.enabled = NO;
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"unlogin" object:nil];
            NSString *errorStr = [NSString stringWithFormat:@"%@",error];
            [self alertAction:@"登陆失败" messageString:errorStr];
        }
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.userIDTextfiled resignFirstResponder];
    [self.passwordTextFiled resignFirstResponder];
    return YES;
}
-(void)back
{
     [[NSNotificationCenter defaultCenter]removeObserver:self name:@"didlogin" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}




-(void)alertAction:(NSString *)alertstring messageString:(NSString *)messageString
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertstring message:messageString preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
